<template>
    <div class="app-container">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="分类：" prop="cate_id">
                <el-select v-model="queryParams.cate_id" clearable placeholder="请选择分类" size="small"  >
                    <el-option v-for="(item, index) in cateList" :key="index" :label="item.cate_title"
                               :value="item.cate_id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="商品名称" prop="search">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入商品名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_normal_disable"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
                <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">添加商品</el-button>
                <el-button type="primary" icon="el-icon-shopping-cart-full" size="small" @click="shelves(1)">批量上架</el-button>
                <el-button type="primary" icon="el-icon-shopping-cart-2" size="small" @click="shelves(2)">批量下架</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table :data="tableData" @selection-change="handleSelectionChange" :row-style="{ height: '57px'}" style="width: 100%;font-size: 12px">
            <el-table-column type="selection" width="55"/>
            <el-table-column prop="goods_id" label="商品ID" width="75"/>
            <el-table-column label="商品图" width="100">
                <template slot-scope="{row}">
                    <el-image :src="row.goods_img" style="width: 36px;height: 36px"></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="goods_name" label="商品名称">
            </el-table-column>
            <el-table-column label="商品分类" align="center" prop="cate_id" width="100">
                <template slot-scope="{row}">
                    {{ row.cate_info? row.cate_info.cate_title : "" }}
                </template>
            </el-table-column>
            <el-table-column prop="price" label="商品售价" width="100"></el-table-column>
            <el-table-column prop="sales" label="销量" width="100"></el-table-column>
            <el-table-column prop="stock" label="库存" width="100"></el-table-column>
            <el-table-column label="状态" width="100">
                <template slot-scope="{row}">
                    <el-tag type="success" v-if="row.is_show == 1">上架</el-tag>
                    <el-tag type="danger" v-if="row.is_show == 2">下架</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                width="120">
                <template slot-scope="{row}">
                    <el-button @click="handleEdit(row)" type="text" size="small" >编辑</el-button>
                    <el-button @click="handleDel(row)" type="text" size="small" >删除</el-button>
                    <el-button @click="handleRecover(row)" type="text" size="small" >恢复</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />

    </div>
</template>
<script>


import {getCateList,getCateSelectTreelists} from "@/api/grzq/mall/category"; //引入AMapLoader
import {getGoodsList} from "@/api/grzq/mall/goods";


import { getAllCom} from "@/api/grzq/community.js";
import {getApparatusList,getApparatusJundaAllList,updateBdJdApparatus}from "@/api/grzq/apparatus.js";

import {removeFrom} from "@/utils";

export default {
    name: "Apparatus",
    dicts:['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            tableData: [],
            cateList: [],
            total: 0,
            // 弹出层标题
            title: "绑定设备",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            equipmentList:[],
            form:{},
            rules:{

            },

        };
    },
    created() {
        this.getCateListTree();
        this.getList();
    },
    methods: {
        /** 查询列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getGoodsList(this.queryParams);
            that.tableData = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },
        /** 查询列表 */
        async getCateListTree() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getCateSelectTreelists(this.queryParams);
            that.cateList = data;

        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {

            };
            this.resetForm("form");
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
            this.$router.push({
                'path':`/mall/goodsdetail`,
                'query': {id:0},
            });
        },
        /** 新增按钮操作 */
        handleEdit(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
            this.$router.push({
                'path':`/mall/goodsdetail`,
                'query': {id:row.goods_id},
            });
        },
        handleDel(row){

        },
        handleRecover(row){

        },
        // 更多操作触发
        handleCommand(command, row) {
            var query = {};
            if (row.water_id) query.id = row.water_id;
            switch (command) {
                case "detail":
                    this.$router.push({
                        'path':`/comm/waterdetail`,
                        'query': query,
                    });
                    break;
                case "editBd":
                    this.getApparatusJundaList()
                    this.open = true
                    this.form = row
                    this.$forceUpdate()
                    console.log("row",row)
                    this.form.mineral_title = row.mineral_title?row.mineral_title:row.water_title
                    this.form.rich_title = row.rich_title?row.rich_title:row.water_title+'富氢水'

                    console.log("this",this.form)
                    break;
                case "electricity_water":
                    this.handleElectricityWater(row);
                    this.$router.push({
                        'path':`/comm/hydropower`,
                        'query': query,
                    });
                    break;
                case "equipmentParams":
                    this.$router.push({
                        'path':`/comm/equipmentParams`,
                        'query': query,
                    });
                    break;
                default:
                    break;
            }
        },


        async getApparatusJundaList() {
            const {data} = await getApparatusJundaAllList();
            this.equipmentList = data
        },
        handleElectricityWater(row){

        },
        handleSelectionChange(val) {
            this.goodsIds = [];
            val.forEach(item => {
                this.goodsIds.push(item.id)
            })
        },

        submitForm: function() {
            // 绑定设备信息
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.water_id != undefined) {
                        const jdform ={
                            water_id : this.form.water_id,
                            water_title : this.form.water_title,
                            equipment_id : this.form.equipment_id,
                            equipment_id_rich : this.form.equipment_id_rich,
                            mid : this.form.mid,
                            mid_rich : this.form.mid_rich,
                            mineral_title : this.form.mineral_title,
                            rich_title : this.form.rich_title,
                        };

                        updateBdJdApparatus(jdform).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        })
                    } else {

                    }
                }
            })
        },

    }
};
</script>
